Hamlib  4.5~git
misc.h
1 /*
2  * Hamlib Interface - toolbox header
3  * Copyright (c) 2000-2004 by Stephane Fillod
4  *
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  */
21 
22 #ifndef _MISC_H
23 #define _MISC_H 1
24 
25 #include <hamlib/rig.h>
26 
27 
28 /*
29  * Careful!! These macros are NOT reentrant!
30  * ie. they may not be executed atomically,
31  * thus not ensure mutual exclusion.
32  * Fix it when making Hamlib reentrant! --SF
33  */
34 #define set_transaction_active(rig) {(rig)->state.transaction_active = 1;}
35 #define set_transaction_inactive(rig) {(rig)->state.transaction_active = 0;}
36 
37 __BEGIN_DECLS
38 
39 /*
40  * Do a hex dump of the unsigned char array.
41  */
42 
43 void dump_hex(const unsigned char ptr[], size_t size);
44 
45 /*
46  * BCD conversion routines.
47  *
48  * to_bcd() converts a long long int to a little endian BCD array,
49  * and return a pointer to this array.
50  *
51  * from_bcd() converts a little endian BCD array to long long int
52  * representation, and return it.
53  *
54  * bcd_len is the number of digits in the BCD array.
55  */
56 extern HAMLIB_EXPORT(unsigned char *) to_bcd(unsigned char bcd_data[],
57  unsigned long long freq,
58  unsigned bcd_len);
59 
60 extern HAMLIB_EXPORT(unsigned long long) from_bcd(const unsigned char
61  bcd_data[],
62  unsigned bcd_len);
63 
64 /*
65  * same as to_bcd() and from_bcd(), but in Big Endian mode
66  */
67 extern HAMLIB_EXPORT(unsigned char *) to_bcd_be(unsigned char bcd_data[],
68  unsigned long long freq,
69  unsigned bcd_len);
70 
71 extern HAMLIB_EXPORT(unsigned long long) from_bcd_be(const unsigned char
72  bcd_data[],
73  unsigned bcd_len);
74 
75 extern HAMLIB_EXPORT(size_t) to_hex(size_t source_length,
76  const unsigned char *source_data,
77  size_t dest_length,
78  char *dest_data);
79 
80 extern HAMLIB_EXPORT(double) morse_code_dot_to_millis(int wpm);
81 extern HAMLIB_EXPORT(int) dot10ths_to_millis(int dot10ths, int wpm);
82 extern HAMLIB_EXPORT(int) millis_to_dot10ths(int millis, int wpm);
83 
84 extern HAMLIB_EXPORT(int) sprintf_freq(char *str, int len, freq_t);
85 
86 /* flag that determines if AI mode should be restored on exit on
87  applicable rigs - See rig_no_restore_ai() */
88 extern int no_restore_ai;
89 
90 /* check if it's any of CR or LF */
91 #define isreturn(c) ((c) == 10 || (c) == 13)
92 
93 
94 /* needs config.h included beforehand in .c file */
95 #ifdef HAVE_INTTYPES_H
96 # include <inttypes.h>
97 #endif
98 
99 #ifdef HAVE_SYS_TIME_H
100 # include <sys/time.h>
101 #endif
102 
103 extern HAMLIB_EXPORT(int) rig_check_cache_timeout(const struct timeval *tv,
104  int timeout);
105 
106 extern HAMLIB_EXPORT(void) rig_force_cache_timeout(struct timeval *tv);
107 
108 extern HAMLIB_EXPORT(setting_t) rig_idx2setting(int i);
109 
110 extern HAMLIB_EXPORT(int) hl_usleep(rig_useconds_t usec);
111 
112 extern HAMLIB_EXPORT(double) elapsed_ms(struct timespec *start, int start_flag);
113 
114 extern HAMLIB_EXPORT(vfo_t) vfo_fixup(RIG *rig, vfo_t vfo, split_t split);
115 extern HAMLIB_EXPORT(vfo_t) vfo_fixup2a(RIG *rig, vfo_t vfo, split_t split, const char *func, const int line);
116 #define vfo_fixup(r,v,s) vfo_fixup2a(r,v,s,__func__,__LINE__)
117 
118 extern HAMLIB_EXPORT(int) parse_hoststr(char *hoststr, char host[256], char port[6]);
119 
120 extern HAMLIB_EXPORT(uint32_t) CRC32_function(uint8_t *buf, uint32_t len);
121 
122 extern HAMLIB_EXPORT(char *)date_strget(char *buf, int buflen, int localtime);
123 
124 #ifdef PRId64
125 
126 # define PRIll PRId64
127 # define PRXll PRIx64
128 #else
129 # ifdef FBSD4
130 # define PRIll "qd"
131 # define PRXll "qx"
132 # else
133 # define PRIll "lld"
134 # define PRXll "lld"
135 # endif
136 #endif
137 
138 #ifdef SCNd64
139 
140 # define SCNll SCNd64
141 # define SCNXll SCNx64
142 #else
143 # ifdef FBSD4
144 # define SCNll "qd"
145 # define SCNXll "qx"
146 # else
147 # define SCNll "lld"
148 # define SCNXll "llx"
149 # endif
150 #endif
151 
152 #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
153 void errmsg(int err, char *s, const char *func, const char *file, int line);
154 #define ERRMSG(err, s) errmsg(err, s, __func__, __FILENAME__, __LINE__)
155 #define ENTERFUNC rig_debug(RIG_DEBUG_VERBOSE, "%s(%d):%s entered\n", __FILENAME__, __LINE__, __func__)
156 // we need to refer to rc just once as it
157 // could be a function call
158 #define RETURNFUNC(rc) do { \
159  int rctmp = rc; \
160  rig_debug(RIG_DEBUG_VERBOSE, "%s(%d):%s return(%ld) %s\n", __FILENAME__, __LINE__, __func__, (long int) (rctmp), rctmp<0?rigerror(rctmp):""); \
161  return (rctmp); \
162  } while(0)
163 
164 #define CACHE_RESET {\
165  elapsed_ms(&rig->state.cache.time_freqMainA, HAMLIB_ELAPSED_INVALIDATE);\
166  elapsed_ms(&rig->state.cache.time_freqMainB, HAMLIB_ELAPSED_INVALIDATE);\
167  elapsed_ms(&rig->state.cache.time_freqSubA, HAMLIB_ELAPSED_INVALIDATE);\
168  elapsed_ms(&rig->state.cache.time_freqSubB, HAMLIB_ELAPSED_INVALIDATE);\
169  elapsed_ms(&rig->state.cache.time_vfo, HAMLIB_ELAPSED_INVALIDATE);\
170  elapsed_ms(&rig->state.cache.time_modeMainA, HAMLIB_ELAPSED_INVALIDATE);\
171  elapsed_ms(&rig->state.cache.time_modeMainB, HAMLIB_ELAPSED_INVALIDATE);\
172  elapsed_ms(&rig->state.cache.time_modeMainC, HAMLIB_ELAPSED_INVALIDATE);\
173  elapsed_ms(&rig->state.cache.time_modeSubA, HAMLIB_ELAPSED_INVALIDATE);\
174  elapsed_ms(&rig->state.cache.time_modeSubB, HAMLIB_ELAPSED_INVALIDATE);\
175  elapsed_ms(&rig->state.cache.time_modeSubC, HAMLIB_ELAPSED_INVALIDATE);\
176  elapsed_ms(&rig->state.cache.time_widthMainA, HAMLIB_ELAPSED_INVALIDATE);\
177  elapsed_ms(&rig->state.cache.time_widthMainB, HAMLIB_ELAPSED_INVALIDATE);\
178  elapsed_ms(&rig->state.cache.time_widthMainC, HAMLIB_ELAPSED_INVALIDATE);\
179  elapsed_ms(&rig->state.cache.time_widthSubA, HAMLIB_ELAPSED_INVALIDATE);\
180  elapsed_ms(&rig->state.cache.time_widthSubB, HAMLIB_ELAPSED_INVALIDATE);\
181  elapsed_ms(&rig->state.cache.time_widthSubC, HAMLIB_ELAPSED_INVALIDATE);\
182  elapsed_ms(&rig->state.cache.time_ptt, HAMLIB_ELAPSED_INVALIDATE);\
183  elapsed_ms(&rig->state.cache.time_split, HAMLIB_ELAPSED_INVALIDATE);\
184  }
185 
186 __END_DECLS
187 
188 #endif /* _MISC_H */
double freq_t
Frequency type,.
Definition: rig.h:386
void rig_force_cache_timeout(struct timeval *tv)
Helper for forcing cache timeout next call.
Definition: misc.c:1756
int sprintf_freq(char *str, int len, freq_t)
Pretty print a frequency.
Definition: misc.c:385
setting_t rig_idx2setting(int i)
Definition: misc.c:754
int dot10ths_to_millis(int dot10ths, int wpm)
Convert duration of tenths of morse code dots to milliseconds at the given speed. ...
Definition: misc.c:351
int hl_usleep(rig_useconds_t usec)
provide sleep and usleep replacements
Definition: sleep.c:53
unsigned char * to_bcd(unsigned char bcd_data[], unsigned long long freq, unsigned bcd_len)
Convert from binary to 4-bit BCD digits, little-endian.
Definition: misc.c:146
The Rig structure.
Definition: rig.h:2519
unsigned long long from_bcd_be(const unsigned char bcd_data[], unsigned bcd_len)
Convert 4-bit BCD digits to binary, big-endian.
Definition: misc.c:273
unsigned char * to_bcd_be(unsigned char bcd_data[], unsigned long long freq, unsigned bcd_len)
Convert from binary to 4-bit BCD digits, big-endian.
Definition: misc.c:230
unsigned long long from_bcd(const unsigned char bcd_data[], unsigned bcd_len)
Convert BCD digits, little-endian, to a long long (e.g. frequency in Hz)
Definition: misc.c:193
Hamlib rig data structures.
double morse_code_dot_to_millis(int wpm)
Convert duration of one morse code dot (element) to milliseconds at the given speed.
Definition: misc.c:338
split_t
Split mode.
Definition: rig.h:375
__BEGIN_DECLS void dump_hex(const unsigned char ptr[], size_t size)
Do a hex dump of the unsigned char array.
Definition: debug.c:90
unsigned int vfo_t
VFO definition.
Definition: rig.h:446
uint64_t setting_t
Setting.
Definition: rig.h:1040
int rig_check_cache_timeout(const struct timeval *tv, int timeout)
Helper for checking cache timeout.
Definition: misc.c:1702
int millis_to_dot10ths(int millis, int wpm)
Convert duration in milliseconds to tenths of morse code dots at the given speed. ...
Definition: misc.c:364